//
//  MSHomeCell.m
//  SocietySale
//
//  Created by Mike on 16/11/7.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSHomeCell.h"

@interface MSHomeCell ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *typeLabel;
@property (nonatomic, strong) UILabel *stateLabel;
@property (nonatomic, strong) UILabel *addressLabel;
@property (nonatomic, strong) UIView  *bgView;
@end

@implementation MSHomeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initView];
    }
    return self;
}

- (void)initView {
    self.titleLabel = [self creatLabel:CGRectMake(12, 10, SCREEN_WIDTH, 20)];
    self.titleLabel.font = [UIFont systemFontOfSize:16];
    self.titleLabel.textColor = [UIColor blackColor];
    
    self.typeLabel = [self creatLabel:CGRectMake(12, self.titleLabel.bottom+8, SCREEN_WIDTH, 18)];
    self.typeLabel.textColor = kYellowColor;
    self.typeLabel.layer.borderColor = kYellowColor.CGColor;
    self.typeLabel.layer.borderWidth = 0.6;
    self.typeLabel.layer.cornerRadius = 2;
    self.typeLabel.textAlignment = NSTextAlignmentCenter;
    
    self.stateLabel = [self creatLabel:CGRectMake(0, self.titleLabel.bottom+7, SCREEN_WIDTH, 20)];
    self.addressLabel = [self creatLabel:CGRectMake(SCREEN_WIDTH -152, self.titleLabel.bottom+7, 140, 20)];
    self.addressLabel.textAlignment = NSTextAlignmentRight;
    self.introductionLabel = [self creatLabel:CGRectMake(12, self.addressLabel.bottom+5, SCREEN_WIDTH-24, 40)];
    self.introductionLabel.numberOfLines = 0;
    self.introductionLabel.font = HeitiFont(14);
    self.introductionLabel.textColor = kDarkGrayColor;
    
    _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 116, SCREEN_WIDTH, 8)];
    _bgView.backgroundColor = kDefaultViewBackgroundColor;
    [self.contentView addSubview:_bgView];

    UIView *horizonLineView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.6)];
    horizonLineView.backgroundColor = kNormalGray;
    [_bgView addSubview:horizonLineView];
}

//创建label
- (UILabel*)creatLabel:(CGRect)frame{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.font = HeitiFont(12);
    label.textColor = kDarkGrayColor;
    [self.contentView addSubview:label];
    return label;
}

- (void)setModel:(MSProjectModel *)model{
    _model = model;
    NSAttributedString *spaceAttr = [[NSAttributedString alloc] initWithString:@" "];
    
    self.titleLabel.text = model.title;
    self.typeLabel.text = model.projectType;
    CGSize typeSize = [HDUtil getSizeWithText:model.projectType font:HeitiFont(14)];
    self.typeLabel.width = typeSize.width;
    
    self.stateLabel.left = self.typeLabel.right + 10;
    self.stateLabel.text = [NSString stringWithFormat:@"%@  %@",model.projectState,model.investment];
    
    NSString *timeStr = model.time;
    NSString *addressStr = [NSString stringWithFormat:@"%@  ",model.address];
    NSString *rightStr = [NSString stringWithFormat:@"%@  %@",model.address,timeStr];
    NSMutableAttributedString *addressAttstr = [[NSMutableAttributedString alloc] initWithString:rightStr attributes:@{NSFontAttributeName:HeitiFont(12),NSForegroundColorAttributeName:kDarkGrayColor}];
    
    if (model.address.length) {
        NSTextAttachment *textAttachment = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
        textAttachment.bounds = CGRectMake(0, -2.5, 14, 14);
        textAttachment.image = [UIImage imageNamed:@"loacation"];
        NSAttributedString *attachmentString = [NSAttributedString attributedStringWithAttachment:textAttachment];
        [addressAttstr insertAttributedString:attachmentString atIndex:0];
    }
    else{
        [addressAttstr insertAttributedString:spaceAttr atIndex:0];
    }
    
    NSTextAttachment *textAttachment2 = [[NSTextAttachment alloc] initWithData:nil ofType:nil];
    textAttachment2.bounds = CGRectMake(0, -2.5, 14, 14);
    textAttachment2.image = [UIImage imageNamed:@"time"];
    NSAttributedString *attachmentString2 = [NSAttributedString attributedStringWithAttachment:textAttachment2];
    [addressAttstr insertAttributedString:attachmentString2 atIndex:addressStr.length+1];
    
//    NSAttributedString *timeAtt = [[NSAttributedString alloc] initWithString:model.time attributes:@{NSFontAttributeName:HeitiFont(12),NSForegroundColorAttributeName:kDarkGrayColor}];
//    [addressAttstr appendAttributedString:timeAtt];
//    
    self.addressLabel.attributedText = addressAttstr;

    self.introductionLabel.text = model.introduction;
    CGSize introductionSize = [HDUtil sizeWithString:model.introduction font:HeitiFont(14) size:CGSizeMake(SCREEN_WIDTH-24, 100)];
    if (introductionSize.height>20) {
        _bgView.top = 112;
        self.introductionLabel.height = 40;
    }
    else{
        _bgView.top = 92;
        self.introductionLabel.height = 20;
    }
    if (self.introductionLabel.hidden) {
        _bgView.top = 67;
    }
}

+ (CGFloat)cellHeightWithModel:(MSProjectModel*)model{
    CGSize introductionSize = [HDUtil sizeWithString:model.introduction font:HeitiFont(14) size:CGSizeMake(SCREEN_WIDTH-24, 100)];
    if (introductionSize.height>20) {
        return 120;
    }
    else{
        return 100;
    }
}

@end
